<template>
	<view class="u-wrap">
		<u-toast ref="uToast"></u-toast>
		<view class="u-search-box">
			<view class="search flex flex_bet flex_align">
				<u-search placeholder="搜索" v-model="search" :show-action="false" :search_sty="true"
					searchClass="no_border_r" borderColor="rgb(230, 230, 230)" shape="round" bgColor="#fff">
				</u-search>
				<view class="search_btn" @click="searchBtn">
					<text>搜索</text>
				</view>
			</view>
		</view>
		<view class="mar_bottom">
			<u-tabs :list="tabList" :activeStyle="{color: '#2a2a2c',fontSize:'28rpx'}" itemStyle="height: 35px;" @click="getTabs"></u-tabs>
		</view>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in productList" :key="index" class="u-tab-item"
					:class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
					@tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.name}}</text>
				</view>
			</scroll-view>
			<block v-for="(item,index) in productList" :key="index">
				<scroll-view scroll-y class="right-box" v-if="current==index">
					<view class="page-view">
						<view class="class-item">
							<view class="item-container">
								<view class="thumb-box" v-for="(item1, index1) in mountingsList" :key="index1"
									@click="goToShop(item1.wx_goods_id)">
									<image class="item-menu-image" :src="item1.img" mode=""></image>
									<view class="money flex flex_align"><text>¥</text><text>{{item1.price}}</text>
										<u-icon v-if="item1.wx_goods_id" name="shopping-cart" size="20" color="#E23043"></u-icon>
									</view>
									<view class="item-menu-name text_hide">{{item1.name}}</view>
								</view>
							</view>
							<view class="" v-if="!mountingsList.length">
								<u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png"></u-empty>
							</view>
						</view>
					</view>
				</scroll-view>
			</block>
		</view>
	</view>
</template>

<script>
	import {
		productmodelList,
		componentList,
		partsList
	} from '@/api/product.js'
	export default {
		data() {
			return {
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				tabList: [
					{
						name:'平板系列'
					}
					// {
					// name: '配件',
					// }, 
					// {
					// 	name: '附件',
					// },
				],
				tabIndex: 0,
				productList: [],
				mountingsList: [],
				search: ''
			}
		},
		computed: {

		},
		onLoad() {
			this.getproductmodelList()
		},
		onShow() {

		},
		methods: {
			goToShop(id) {
				if (id) {
					wx.navigateTo({
						url: `plugin-private://wx34345ae5855f892d/pages/productDetail/productDetail?productId=${id}`,
					});
				} else {
					this.$refs.uToast.show({
						message: '暂无商品详情',
						type: 'default',
						icon: false
					})
				}
			},
			//搜索按钮
			searchBtn() {
				uni.$u.debounce(this.toNext, 1000)
			},
			toNext() {
				this.switchOverList(this.search)
			},
			getTabs(item) {
				this.tabIndex = item.index
				this.switchOverList()
			},
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			//获取产品型号
			getproductmodelList() {
				productmodelList().then(res => {
					this.productList = res.data
					this.switchOverList()
				}).catch(err => {
					this.productList = []
				})
			},
			switchOverList(search) {
				if (!this.tabIndex) {
					this.getcomponentList(this.productList[this.current].id, search)
				} else {
					this.getpartsList(this.productList[this.current].id, search)
				}
			},
			//获取配件列表
			getcomponentList(id, search = '') {
				componentList({
					product_model_id: id,
					search: search
				}).then(res => {
					this.mountingsList = res.data
				}).catch(err => {
					this.mountingsList = []
				})
			},
			//获取附件列表
			getpartsList(id, search = '') {
				partsList({
					product_model_id: id,
					search: search
				}).then(res => {
					this.mountingsList = res.data
				}).catch(err => {
					this.mountingsList = []
				})
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if (index == this.current) return;
				this.current = index;
				this.switchOverList()
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx 0 18rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		padding-top: 30rpx;
		width: 250rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 80rpx;
		margin: 20rpx 0;
		background: #f1f2f4;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 24rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #2d5dc1;
		font-size: 26rpx;
		background: #fff;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
		//border-left: 4px solid #000;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}

	.right-box {
		border-top-left-radius: 40rpx;
		border-top-right-radius: 40rpx;
		overflow: hidden;
		background-color: rgb(250, 250, 250);
	}


	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 20rpx;
	}

	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
		text-align: center;
		width: -webkit-fill-available;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 40rpx;
	}

	.item-menu-image {
		width: 100rpx;
		height: 100rpx;
	}

	.money {
		font-size: 28rpx;
		color: #E23043;
		padding: 0 20rpx;
		line-height: 56rpx;
		border-radius: 12rpx;
		box-shadow: 1px 2px 4px hsl(220, 60%, 95%);
		margin: 10rpx 0;

		text {
			font-size: 24rpx;
		}
	}
</style>
